#include "SettingWidget.h"
#include <QIcon>

#include <QPainter>
#include <QApplication>
#include <QRandomGenerator>

namespace UserManagementSystem {
SettingWidget::SettingWidget(QWidget *parent)
    :FramelessWidget(parent)
{
    qDebug()<<__FUNCTION__;

    this->setFixedSize(700,500);

    m_mainLayout=new QVBoxLayout(this);


    m_subLayout=new QHBoxLayout();

    m_closeBtn=new QPushButton();
    m_titleLabel.setText(tr("设置"));

    m_leftBtnsLayout=new QVBoxLayout();
    m_btnsGroup=new QButtonGroup();

    m_stackedContentWidget=new QStackedWidget();

    m_generalBtn=new QPushButton(tr("通用"),nullptr);
    m_memoryBtn=new QPushButton(tr("存储"),nullptr);
    m_feedbackBtn=new QPushButton(tr("反馈"),nullptr);
    m_aboutBtn=new QPushButton(tr("关于"),nullptr);

    m_generalWidget=new Setting_General();
    m_memoryWidget=new Setting_Memory();
    m_feedBackWidget=new Setting_Feedback();
    m_aboutWidget=new Setting_About();



    InitDefaultLayout();

    BindSignalAndSlot();
}

void SettingWidget::On_General_Btn_Clicked()
{
    m_stackedContentWidget->setCurrentIndex(0);
    qDebug()<<"点击了通用窗口";
}

void SettingWidget::On_Memory_Btn_Clicked()
{
    m_stackedContentWidget->setCurrentIndex(1);
    qDebug()<<"点击了存储窗口";
}

void SettingWidget::On_Feedback_Btn_Clicked()
{
    m_stackedContentWidget->setCurrentIndex(2);
    qDebug()<<"点击了反馈窗口";
}

void SettingWidget::On_About_Btn_Clicked()
{
    m_stackedContentWidget->setCurrentIndex(3);
    qDebug()<<"点击了关于窗口";
}

void SettingWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing);



    QColor white(255,255,255);//带透明度的蓝色
    painter.setBrush(white);
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(this->rect(),10,10);
}

void SettingWidget::InitDefaultStyle()
{
    QString close_path=QApplication::applicationDirPath()+"/res/PatientManager/close.png";
    QString close_path_hover=QApplication::applicationDirPath()+"/res/PatientManager/close_hover.png";

    QString close_style=QString(R"(
    QPushButton {
        border: none;
        image: url(%1);
    }
    QPushButton:hover {
        image: url(%2);
    }
    QPushButton:pressed {
        image: url(%2);
    }
)").arg(close_path).arg(close_path_hover);

    m_closeBtn->setStyleSheet(close_style);


    m_HLine.setFrameShape(QFrame::HLine);
    m_HLine.setLineWidth(1);

    m_VLine.setFrameShape(QFrame::VLine);
    m_VLine.setLineWidth(1);

    m_HLine.setStyleSheet("color:#9a9a9e;");
    m_VLine.setStyleSheet("color:#9a9a9e;");

    //按钮组的样式,默认无边框，hover和点击的时候字体和颜色变蓝色
    QString btns_style=(R"(
    QPushButton {
        background-color: white;
        height: 20px;
        width: 40px;
        color:  black ;
        border: none;
        padding: 5px 10px;
    }
    QPushButton:hover,QPushButton:pressed{
        background-color: #f7f9fb;
        color:#4672C4;
}

)");

    //设置label的样式
    m_titleLabel.setStyleSheet("font-size:13pt;color: black; font-weight: bold;");


    m_generalBtn->setStyleSheet(btns_style);
    m_memoryBtn->setStyleSheet(btns_style);
    m_feedbackBtn->setStyleSheet(btns_style);
    m_aboutBtn->setStyleSheet(btns_style);
}

void SettingWidget::InitDefaultLayout()
{


    m_stackedContentWidget->addWidget(m_generalWidget);
    m_stackedContentWidget->addWidget(m_memoryWidget);
    m_stackedContentWidget->addWidget(m_feedBackWidget);
    m_stackedContentWidget->addWidget(m_aboutWidget);

    QHBoxLayout* label_layout=new QHBoxLayout();
    label_layout->addWidget(&m_titleLabel);
    label_layout->addStretch(1);
    label_layout->addWidget(m_closeBtn);

    QFrame* btns_frame=new QFrame();
    btns_frame->setFixedWidth(100);
    btns_frame->setLayout(m_leftBtnsLayout);

    m_leftBtnsLayout->addWidget(m_generalBtn);
    m_leftBtnsLayout->addWidget(m_memoryBtn);
    m_leftBtnsLayout->addWidget(m_feedbackBtn);
    m_leftBtnsLayout->addWidget(m_aboutBtn);
    m_leftBtnsLayout->addStretch(4);

    m_subLayout->addWidget(btns_frame);
    m_subLayout->addWidget(&m_VLine);
    m_subLayout->addWidget(m_stackedContentWidget);
    m_subLayout->setContentsMargins(10,10,10,10);

    m_mainLayout->addLayout(label_layout);
    m_mainLayout->addSpacing(10);
    m_mainLayout->addWidget(&m_HLine);
    m_mainLayout->addLayout(m_subLayout);
    m_mainLayout->setContentsMargins(20,20,20,20);


    InitDefaultStyle();
}

void SettingWidget::BindSignalAndSlot()
{
    //关闭窗口按钮

    connect(m_closeBtn,&QPushButton::clicked,this,[this](){

        this->close();
    });

    //通用按钮点击
    connect(m_generalBtn,&QPushButton::clicked,this,&SettingWidget::On_General_Btn_Clicked);

    connect(m_memoryBtn,&QPushButton::clicked,this,&SettingWidget::On_Memory_Btn_Clicked);

    connect(m_feedbackBtn,&QPushButton::clicked,this,&SettingWidget::On_Feedback_Btn_Clicked);

    connect(m_aboutBtn,&QPushButton::clicked,this,&SettingWidget::On_About_Btn_Clicked);
}



}
